<?php

class App_Util_Tabs {
	
	private $tabs;
	private $marcaCompleto;
	private $actual = "";
	function __construct() {
		$this->tabs = array ();
		$this->marcaCompleto = array ();
	}
	
	function agregar_vista($texto_tab, $pagina_enlace, $parametros = "", $title="") {
		$this->tabs [$texto_tab] ["contenido"] = $pagina_enlace;
		$this->tabs [$texto_tab] ["param"] = $parametros;
		$this->tabs [$texto_tab] ["tipo"] = "vista";
		$this->tabs [$texto_tab] ["actual"] = "No";
		$this->tabs [$texto_tab] ["check"] = "No";
		$this->tabs [$texto_tab] ["title"] = $title;
	}
	
	function agregar_contenido($texto_tab, $contenido = "") {
		$this->tabs [$texto_tab] ["contenido"] = $contenido;
		$this->tabs [$texto_tab] ["param"] = "";
		$this->tabs [$texto_tab] ["tipo"] = "contenido";
		$this->tabs [$texto_tab] ["actual"] = "No";
		//$this->tabs [$texto_tab] ["check"] = "No";
	}
	
	function eliminar_tab($texto_tab) {
		unset ( $this->tabs [$texto_tab] );
		if ($this->actual == $texto_tab)
			$this->actual = "";
	}
	
	function checkear($texto_tab) {
		$this->tabs [$texto_tab] ["check"] = "Si";
	}
	
	function actual($texto_tab) {
		$this->actual = $texto_tab;
	}
	
	function contenido_tab($texto_tab, $contenido) {
		$this->tabs [$texto_tab] ["contenido"] = $contenido;
		$this->tabs [$texto_tab] ["tipo"] = "contenido";
	}
	
	function contenido($contenido) {
		$this->tabs [$this->actual] ["contenido"] = $contenido;
		$this->tabs [$this->actual] ["tipo"] = "contenido";
	}
	
	function renderElement($orientacion="") {
		$baseUrl = Zend_Controller_Front::getInstance ()->getBaseUrl();
		
		$DivName="{$orientacion}Tabs";
		$Div=" id=\"{$DivName}\"";
		$ul="<ul>\n";
		$ul_="</ul>\n";
		$li="<li>\n";
		$li_="</li>\n";
		$arr_keys = array_keys ( $this->tabs );
		$tabActual = 0;
		$tabDisable = array();
		$menuTab = "<div{$Div}>\n";
		$menuTab .= "{$ul}";
		for($i = 0; $i < count ( $this->tabs ); $i ++) {
			$parametros = $this->tabs [$arr_keys [$i]] ["param"];
			$tipo = $this->tabs [$arr_keys [$i]] ["tipo"];
			$contenido = $this->tabs [$arr_keys [$i]] ["contenido"];
			$actual = $this->tabs [$arr_keys [$i]] ["actual"];
			$checkeado = $this->tabs [$arr_keys [$i]] ["check"];
			$title = $this->tabs [$arr_keys [$i]] ["title"];
			if ($parametros != "") {
				$contenido .= "{$parametros}";
			}
			if (($actual == "Si") || ($this->actual == $arr_keys [$i])) {
				$tabActual = $i;
			}
			$imagencheckeado="";
			if("Si"==$checkeado){
//				$imagencheckeado=" <img src=\"{$baseUrl}/img/success.png\" align=\"absmiddle \">";
				$imagencheckeado=" <span class=\"glyphicon glyphicon-ok\"></span>";
			}
			if ("vista" == $tipo) {
				if ($contenido != "#") {
					$menuTab .= "{$li}<a href=\"#{$DivName}_{$i}\" onclick=\"javascript:openpage('{$baseUrl}{$contenido}');\" title=\"{$title}\">{$arr_keys[$i]}{$imagencheckeado}</a>{$li_}\n";
				} else {
				    //AQUI DESHABILITAR EL TAB
					$menuTab .= "{$li}<a href=\"#\">{$arr_keys[$i]}{$imagencheckeado}</a>{$li_}\n";
					$tabDisable[] = $i;
				}
			} else {
				$menuTab .= "{$li}<a href=\"#{$DivName}_{$i}\">{$arr_keys[$i]}{$imagencheckeado}</a>{$li_}\n";
			}
		}
		$menuTab .= "{$ul_}";
		for($i = 0; $i < count ( $this->tabs ); $i ++) {
			$tipo = $this->tabs [$arr_keys [$i]] ["tipo"];
			$contenido = $this->tabs [$arr_keys [$i]] ["contenido"];
			$menuTab .= "<div id=\"{$DivName}_{$i}\">\n";
			if ("contenido" == $tipo) {
				$menuTab .= "{$contenido}";
			} elseif ($contenido != "#") {
				$menuTab .= "<br/><br/><br/><img src=\"{$baseUrl}/img/loadingAnimation.gif\"/><br/><br/><br/>";
			}
			$menuTab .= "</div>\n";
		}
		$menuTab .= "</div>\n";
		$menuTab .= "<script type=\"text/javascript\">\n";
		//if ($tabActual > 0) {
			$menuTab .= "var tabs = $(\"#{$DivName}\").tabs();\n";
			$menuTab .= "tabs.tabs('option','active',{$tabActual});\n";
    		$indices = implode(",", $tabDisable);
    		$menuTab .= "tabs.tabs('option','disabled',[{$indices}]);\n";
		//}
		if ("horizontal"!=$orientacion){
			$menuTab .= "$(\"#{$DivName}\").tabs().addClass('ui-tabs-{$orientacion} ui-helper-clearfix');\n";
			//$menuTab .= "$(\"#{$DivName}\").removeClass('ui-corner-top').addClass('ui-corner-left');\n";
		}
		/*$menuTab .= "$(function() {\n";
		$menuTab .= "$(\"#{$DivName}\").tabs().show();\n";
		$menuTab .= "});\n";*/
		$menuTab .= "function openpage(url){\n
		    location.href=url;\n
		}\n
		\n";
		$menuTab .= "</script>\n";
		return $menuTab;
	}
	
}

?>